<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>TRY is greedy - Boost.Outcome documentation</title>
<link rel="stylesheet" href="../../../css/boost.css" type="text/css">
<meta name="generator" content="Hugo 0.52 with Boostdoc theme">
<meta name="viewport" content="width=device-width,initial-scale=1.0"/>

<link rel="icon" href="../../../images/favicon.ico" type="image/ico"/>
<body><div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/essential/result/try.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/essential/result.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/essential/result/try_ref.html"><img src="../../../images/next.png" alt="Next"></a></div><div id="content">
  <div class="titlepage"><div><div><h1 style="clear: both">TRY is greedy</h1></div></div></div>
  <p><a href="../../../reference/macros/tryv.html" class="api-reference"><code>BOOST_OUTCOME_TRYV(expr)/BOOST_OUTCOME_TRY(expr)</code></a>
 has &lsquo;greedier&rsquo; implicit conversion semantics than
<a href="../../../reference/types/basic_result.html" class="api-reference"><code>basic_result&lt;T, E, NoValuePolicy&gt;</code></a>
. For example, this code won&rsquo;t compile:</p>
<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="n">outcome</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">error_code</span><span class="o">&gt;</span> <span class="n">test</span><span class="p">(</span><span class="n">outcome</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">errc</span><span class="o">&gt;</span> <span class="n">r</span><span class="p">)</span>
<span class="p">{</span>
    <span class="k">return</span> <span class="n">r</span><span class="p">;</span>  <span class="c1">// you need to use explicit construction here
</span><span class="c1"></span>    <span class="c1">// i.e. return outcome::result&lt;int&gt;(r);
</span><span class="c1"></span><span class="p">}</span>
</code></pre></div>
<p>This is chosen because there is a non-trivial conversion between <code>std::errc</code> and <code>std::error_code</code>,
so even though that conversion is implicit for <code>std::error_code</code>, Outcome does not expose the
implicitness here in order to keep the implicit constructor count low (implicit constructors
add significantly to build times).</p>

<p>The <code>TRY</code> operation is more greedy though:</p>
<div class="highlight"><pre class="chroma"><code class="language-c++" data-lang="c++"><span class="n">outcome</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">error_code</span><span class="o">&gt;</span> <span class="n">test</span><span class="p">(</span><span class="n">outcome</span><span class="o">::</span><span class="n">result</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">errc</span><span class="o">&gt;</span> <span class="n">r</span><span class="p">)</span>
<span class="p">{</span>
    <span class="n">BOOST_OUTCOME_TRY</span><span class="p">(</span><span class="n">r</span><span class="p">);</span>    <span class="c1">// no explicit conversion needed
</span><span class="c1"></span>    <span class="k">return</span> <span class="n">r</span><span class="p">.</span><span class="n">value</span><span class="p">();</span>
<span class="p">}</span>
</code></pre></div>
<p>This is because <code>result&lt;int, std::error_code&gt;</code> will implicitly construct from anything which
either <code>int</code> or <code>std::error_code</code> will implicitly construct from. However,
<code>result&lt;int, std::error_code&gt;</code> will not implicitly construct from <code>result&lt;int, std::errc&gt;</code>.</p>

<p>Thus bear this in mind during usage: <code>TRY</code> is greedier for implicit conversions than the Outcome
types themselves.</p>


        </div><p><small>Last revised: February 03, 2020 at 12:05:24 UTC</small></p>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../../../tutorial/essential/result/try.html"><img src="../../../images/prev.png" alt="Prev"></a>
    <a accesskey="u" href="../../../tutorial/essential/result.html"><img src="../../../images/up.png" alt="Up"></a>
    <a accesskey="h" href="../../../index.html"><img src="../../../images/home.png" alt="Home"></a><a accesskey="n" href="../../../tutorial/essential/result/try_ref.html"><img src="../../../images/next.png" alt="Next"></a></div></body>
</html>
